<template>

  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- <el-button @click="fn">点击</el-button> -->
        <Upload :on-success="upload" />
      </el-card>
    </div>
  </div>
</template>
<script>
import Upload from '@/components/UploadExcel'
import { impors } from '@/api/employees'
export default {
  components: {
    Upload
  },
  data() {
    return {
    }
  },
  methods: {
    async upload(data) {
      data.results = data.results.map(v => {
        return this.zh2en(v)
      })
      await impors(data.results)
      await this.$message('导入成功')
      await this.$router.back()
    },
    zh2en(v) {
      const dict = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      const newarr = {}
      for (const key in v) {
        const a = dict[key]
        let b = v[key]
        if (a === 'timeOfEntry' || a === 'correctionTime') {
          b = new Date(this.formatDate(b, '/'))
        }
        if (a === 'username') {
          b = '张三'
        }
        if (a === 'mobile') {
          b = Math.ceil(Math.random() * 10000000000)
          if (b < 99999999) {
            b += 0
          }
          b += '1'
        }
        newarr[a] = b
      }
      return newarr
    },
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
    // 肉眼编程
    // fn() {
    //   console.log(this.arr)
    //   const arr = {}

    //   const a = []
    //   this.arr.forEach(v => {
    //     for (const key in v) {
    //       key === '名字' ? arr.name = v[key] : ''
    //       key === '年龄' ? arr.old = v[key] : ''
    //       key === '性别' ? arr.gender = v[key] : ''
    //     }
    //     a.push(arr)
    //   })
    //   console.log(a)
    // }
  }

}
</script>
